#include "workspace.h"
#include "math.h"

namespace IAlib
{
    WorkSpace::WorkSpace()
    {
        m_id = "";
    }

    WorkSpace::WorkSpace(const WorkSpace &workspace)
    {
        m_id = workspace.m_id;
        m_description = workspace.m_description;
        m_type = workspace.m_type;
        m_parent = workspace.m_parent;

        m_length = workspace.m_length;
        m_width = workspace.m_width;
        m_height = workspace.m_height;

        m_step = workspace.m_step;

        m_actors = workspace.m_actors;
        m_artefacts = workspace.m_artefacts;
    }

    void WorkSpace::setArea(const float length, const float height, const float width)
    {
        m_length = length;
        m_height = height;
        m_width = width;
    }

    void WorkSpace::addActor(Acteur *actor)
    {
        if (!m_actors.contains(actor->getID()))
        {
            m_actors.insert(actor->getID(),actor);
        }
    }

    void WorkSpace::removeActor(const QString &actor_id)
    {
        if (m_actors.contains(actor_id))
        {
            m_actors.remove(actor_id);
        }
    }

    void WorkSpace::addArtefact(Artefact *artefact)
    {
        if (!m_artefacts.contains(artefact->getID()))
        {
            m_artefacts.insert(artefact->getID(),artefact);
        }
    }

    void WorkSpace::removeArtefact(const QString &artefact_id)
    {
        if (m_artefacts.contains(artefact_id))
        {
            m_artefacts.remove(artefact_id);
        }
    }

    float distance(const Position& pos1, const Position& pos2)
    {
        return sqrt(pow(pos1.m_x-pos2.m_x,2)+pow(pos1.m_y-pos2.m_y,2)+pow(pos1.m_z-pos2.m_z,2));
    }

    //------------------------------------------

    void WorkSpace::updateActorSense(const float d)
    {
        QMapIterator<QString, Acteur*> it(m_actors);
        while (it.hasNext())
        {
            it.next();
            QMapIterator<QString, Acteur*> actor_it(m_actors);
            while (actor_it.hasNext())
            {
                actor_it.next();
                if (distance(actor_it.value()->getPosition(),it.value()->getPosition()) < d)
                {
                    SocialKnowledge kw;
                    kw.setActorID(actor_it.value()->getID());
                    kw.setActorRole(actor_it.value()->getRole());
                    kw.setActorPosition(actor_it.value()->getPosition());
                    kw.setActorState(actor_it.value()->getStat());
                    kw.setLastCheck(m_step);
                    //it.value()->addSocialKnowledge(kw);
                }
            }
            QMapIterator<QString, Artefact*> artefact_it(m_artefacts);
            while (artefact_it.hasNext())
            {
                artefact_it.next();
                if (distance(artefact_it.value()->getPosition(),it.value()->getPosition()) < d)
                {
                    MeansKnowledge kw;
                    kw.setMeansID(artefact_it.value()->getID());
                    kw.setExclusive(artefact_it.value()->isExclusive());
                    kw.setMaxSlot(artefact_it.value()->getMaxSlot());
                    kw.setPosition(artefact_it.value()->getPosition());
                    kw.setLastCheck(m_step);
                    //it.value()->addMeansKnowledge(kw);
                }
            }
        }

    }

    QDomElement WorkSpace::toXML(QDomDocument &doc)
    {
        QDomElement ele = doc.createElement("workspace");
        ele.setAttribute("id", m_id);
        ele.setAttribute("type", m_type);
        ele.setAttribute("parent", m_parent);
        ele.setAttribute("description", m_description);
        ele.setAttribute("length", QString::number(m_length));
        ele.setAttribute("width", QString::number(m_width));
        ele.setAttribute("height", QString::number(m_height));
        return ele;
    }

    void WorkSpace::fromXML(QDomElement ele)
    {
        setID(ele.attribute("id",""));
        setType((WorkSpace::WORK_SPACE_TYPE)ele.attribute("type","").toInt());
        setParent(ele.attribute("parent",""));
        setDescription(ele.attribute("description",""));
        setLength(ele.attribute("length","").toFloat());
        setWidth(ele.attribute("width","").toFloat());
        setHeight(ele.attribute("height","").toFloat());
    }

}
